// pages/my/balance/password/password.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oldPwd: '',
    newPwd: '',
    lastPwd: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var targetstr = 'set';
    if (app.globalData.isPwd == true) {
      targetstr = 'update';
    }
    if (targetstr == 'set') {
      wx.setNavigationBarTitle({
        title: '设置余额使用密码',
      });
    }
    else {
      wx.setNavigationBarTitle({
        title: '修改余额使用密码',
      });
    }
    this.setData({
      target: targetstr
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  forgetPwd: function () {
    var phone = app.globalData.personInfo.MOBILE;
    var msg = '发送（临时密码）到您的激活手机号码：' + phone;
    var that = this;
    wx.showModal({
      title: '',
      content: msg,
      confirmColor: '#a08257',
      confirmText: '短信验证',
      cancelText: '关闭',
      success: function (res) {
        if (res.confirm) {
          console.log('send MSG');
          that.sendMsgAction();
        }
      }
    })
  },
  sendMsgAction: function () {
    var phone = app.globalData.personInfo.MOBILE;
    var userid = app.globalData.userid;
    var url = '/yuanmai_we/user/password/forgot';
    var property = {
      "USERID": userid,
      "MOBILE": phone
    };
    var params = {
      __zaofans: true,
      datas: JSON.stringify(property)
    };
    app.loadDataFromServer(url, this.sendMsgActionCallBack, params, 'post', true);
  },
  sendMsgActionCallBack: function (res) {
    console.log(res);
    if (res.success) {
      wx.showToast({
        title: '发送成功',
      });
    }
  },
  goBack: function () {
    wx.navigateBack({});
  },
  oldPwdInput: function (e) {
    this.setData({
      oldPwd: e.detail.value
    });
  },
  newPwdInput: function (e) {
    this.setData({
      newPwd: e.detail.value
    });
  },
  lastPwdInput: function (e) {
    this.setData({
      lastPwd: e.detail.value
    });
  },
  submitPwd: function () {
    if (this.data.target == 'set' && (this.data.newPwd == '' || this.data.lastPwd == '')) {
      return;
    }
    else if (this.data.target == 'update' && (this.data.oldPwd == '' || this.data.newPwd == '' || this.data.lastPwd == '')) {
      return;
    }
    else{
      if (this.data.newPwd != this.data.lastPwd){
        wx.showToast({
          title: '密码不一致',
          icon: 'none'
        });
        return;
      }
    }
    var url = '/yuanmai_we/user/userinfo/updatepwd';
    var property = {
      "oldPwd": null, 
      "newPwd": this.data.lastPwd
    }
    if (this.data.target == 'update'){
      property.oldPwd = this.data.oldPwd;
    }
    var params = {
      datas: JSON.stringify(property),
      __zaofans: true
    }
    app.loadDataFromServer(url,this.submitPwdCallBack,params,'post',true);
  },
  submitPwdCallBack:function(res){
    console.log(res);
    if (res.result == 'success'){
      wx.showToast({
        title: '设置成功',
        duration: 1000
      });
      var delayid = setTimeout(function(){
        clearTimeout(delayid);
        wx.navigateBack({});
      },1000);
    }
    else{
      wx.showToast({
        title: '设置失败',
        icon: 'none'
      });
    }
  }
})